package com.qs.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qs.mapper.StoreMapper;
import com.qs.mapper.UserMapper;
import com.qs.pojo.dto.CommentDto;
import com.qs.pojo.dto.StoreCommentDto;
import com.qs.pojo.entity.Comment;
import com.qs.pojo.entity.Store;
import com.qs.pojo.entity.StoreComment;
import com.qs.mapper.StoreCommentMapper;
import com.qs.pojo.entity.User;
import com.qs.pojo.vo.StoreCommentVo;
import com.qs.service.IStoreCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.awt.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺评价表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-12-17
 */
@Service
@RequiredArgsConstructor
public class StoreCommentServiceImpl extends ServiceImpl<StoreCommentMapper, StoreComment> implements IStoreCommentService {

    private final UserMapper userMapper;

    private final StoreMapper storeMapper;

    @Override
    public List<CommentDto> listComments(Integer storeId) {
        QueryWrapper<StoreComment> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",storeId);
        wrapper.orderByDesc("create_time");
        List<StoreComment> comments = baseMapper.selectList(wrapper);
        List<CommentDto> list = new ArrayList<>();
        if(!CollectionUtils.isEmpty(comments)){
            comments.stream().forEach(comment -> {
                User user = userMapper.selectById(comment.getUserId());
                CommentDto commentDto = new CommentDto();
                commentDto.setAvatar(user.getAvatar());
                commentDto.setNickName(user.getNickName());
                commentDto.setId(comment.getId());
                commentDto.setComment(comment.getComment());
                String createTime = LocalDateTimeUtil.format(LocalDateTimeUtil.parse(comment.getCreateTime().toString()), DatePattern.NORM_DATETIME_MINUTE_PATTERN);
                commentDto.setCreateTime(createTime);
                list.add(commentDto);
            });
            return list;
        }
        return null;
    }

    @Override
    public Map<String,Object> backendListComments(Integer pageNo, Integer pageSize, StoreCommentDto storeCommentDto) {
        Page<StoreComment> pageInfo = new Page<>(pageNo,pageNo);
        //查询当前用户的店铺id
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",storeCommentDto.getUserId());
        List<Store> stores = storeMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(stores)){
            List<Integer> storeIds = stores.stream().map(item -> item.getId()).collect(Collectors.toList());
            List<CommentDto> list = new ArrayList<>();
            storeIds.stream().forEach(storeId ->{
                QueryWrapper<StoreComment> wrapper = new QueryWrapper<>();
                wrapper.eq("store_id",storeId);
                if(storeCommentDto.getNickName() != null){
                    QueryWrapper<User> wrapper1 = new QueryWrapper<>();
                    wrapper1.eq("nick_name",storeCommentDto.getNickName());
                    User user = userMapper.selectOne(wrapper1);
                    wrapper.eq("user_id",user.getId());
                }
                Page<StoreComment> selectPage = baseMapper.selectPage(pageInfo, wrapper);
                List<StoreComment> comments = selectPage.getRecords();
                if(!CollectionUtils.isEmpty(comments)){
                    comments.stream().forEach(comment -> {
                        User user = userMapper.selectById(comment.getUserId());
                        CommentDto commentDto = new CommentDto();
                        commentDto.setAvatar(user.getAvatar());
                        commentDto.setNickName(user.getNickName());
                        commentDto.setId(comment.getId());
                        commentDto.setComment(comment.getComment());
                        commentDto.setScore(comment.getScore());
                        String createTime = LocalDateTimeUtil.format(LocalDateTimeUtil.parse(comment.getCreateTime().toString()), DatePattern.NORM_DATETIME_MINUTE_PATTERN);
                        commentDto.setCreateTime(createTime);
                        list.add(commentDto);
                    });
                }
            });
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("total",list.size());
            map.put("data",list);
            return map;
        }
        return null;
    }
}
